(function () {

    // 任务查询
    jQuery('.searchTaskBtn').on('click',function(){
        serverTaskData.key=jQuery('#task-key').val();
        serverTaskTable.draw();
    });
  
	// 获取显示审批任务数据 服务器分页
	 var serverTaskData = {};
	 var serverTable;
	    jQuery(document).ready(function () {
	        serverTaskTable = jQuery('#taskTable').DataTable({
	            "serverSide": true,
	            "ordering": false,
	            "ajax": {
	                "dataType": "json",
	                "type": "get",
	                "cache": false,
	                "url":window.rootPath+ '/ui/task/getpage',
	                "data": function (d) {
	                    return $.extend({}, d, serverTaskData);
	                }
	            },
	            "iDisplayLength": 5,
	            columns: [
	                {
	                	//1编号
	                    data: null,
	                    defaultContent: ""
	                },
	                {//2操作
	                    data: "contentUrl",
	                    defaultContent: "",
	                    render: function (data, type, full, meta) {
	                        var operate ='<a  class="btn btn-sm btn-info" href='+window.rootPath+data+'>审批</a>';
	                        return operate;
	                    }
	                },
	                    {//3任务类型
		                    data: 'processDefinitionName'
		                  
	                },
	                {//3任务名称
	                    data: 'taskTitle'
	                  
                    },
	                {//4创建时间
	                    data: 'taskCreateTime',
	                    render:function (data, type, full, meta) {
	                        if (data)
	                            return moment(data).format('YYYY/MM/DD HH:mm');
	                        else
	                            return '';
	                        }
	                },
	                {//5发起人
	                    data: 'starterName'
	                }
	                ],
	            "drawCallback": function (oSettings) {
	                var k = 0;
	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	                }

	                serverTaskTable.off('click', 'tr');
	                serverTaskTable.on('click', 'tr', function (event) {
	                    event = event || window.event;
	                    var target = event.target || event.scrElement;
	                    var data = serverTaskTable.row(this).data();

	                    if (target.className.indexOf('auditBtn') != -1) {
	                        auditTask(data);
	                    }
	                    
	                 
	                    return;
	                });
	            }
	        });
	    });
	    // 审批按钮
        var auditTask=function(data){
            var html=jQuery("#auditTemplate").html();
            var template=_.template(html);
            var g=template({'task':data});    
           
            jQuery("#auditModalContent").html(g);
            jQuery("#auditModal").modal("show");
            
        };
	
    ////待办任务列表
     var myTaskData = {};
   	 var myTaskTable;
   	 jQuery(document).ready(function () {
   		myTaskTable = jQuery('#myTaskTable').DataTable({
	            "serverSide": true,
	            "ordering": false,
	            "ajax": {
	                "dataType": "json",
	                "type": "get",
	                "cache": false,
	                "url":window.rootPath+ '/ui/index/getMyTaskpage',
	                "data": function (d) {
	                    return $.extend({}, d, myTaskData);
	                }
	            },
	           "iDisplayLength": 5,
	            columns: [
	                {
	                    data: null,
	                    defaultContent: ""
	                },
	                {
	                    data: null,
	                    defaultContent: "",
	                    render: function (data, type, full, meta) {
	                        var operate ='<button type="button" class="btn btn-sm btn-info dealMyBtn">处理</button>';
	                        return operate;
	                    }
	                },
	                {
		                data: 'name',
		                
		                  
	                },
	                {
		                data: 'createTime',
		                render:function (data, type, full, meta) {
   	                        if (data)
   	                            return moment(data).format('YYYY/MM/DD HH:mm');
   	                        else
   	                            return '';
   	                    }
		                  
	                }
	               ],
	            "drawCallback": function (oSettings) {
	                var k = 0;
	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	                }
	                   myTaskTable.off('click', 'tr');
	                   myTaskTable.on('click', 'tr', function (event) {
	                    event = event || window.event;
	                    var target = event.target || event.scrElement;
	                    var data = myTaskTable.row(this).data();
	                    if (target.className.indexOf('dealMyBtn') != -1) {
	                    	dealMyTask(data);
	                    }
	                    return;
	                });
	             
	            }
	        });
	    });
   	 var dealMyTask=function(data){
   		var url1=window.rootPath+data.content
   		window.location=url1; 
   	 }
   	 //会签
   	var hqTaskData = {};
  	 var hqTaskTable;
  	 jQuery(document).ready(function () {
  		hqTaskTable = jQuery('#hqTable').DataTable({
	            "serverSide": true,
	            "ordering": false,
	            "ajax": {
	                "dataType": "json",
	                "type": "get",
	                "cache": false,
	                "url":window.rootPath+ '/ui/crmout/BeforeContractCounterSign/listCrm',
	                "data": function (d) {
	                    return $.extend({}, d, hqTaskData);
	                }
	            },
	           "iDisplayLength": 5,
	            columns: [
	                {
	                    data: null,
	                    defaultContent: ""
	                },
	                {
	                    data: 'bContractId',
	                    defaultContent: "",
	                    render: function (data, type, full, meta) {
	                    	var operate ='<a  class="btn btn-sm btn-info" href='+window.rootPath+'/ui/beforecontract/sp/'+data+'?taskId='+data+'>审批</a>';
	                        return operate;
	                    }
	                },
	                {
		                data: 'cContractCode',
	                },
	                {
	                	data: 'cOrderUnitName',
	                },
	                {
	                	data: 'name',
	                },
	                {
	                	data: 'createTime',
		                render:function (data, type, full, meta) {
   	                        if (data)
   	                            return moment(data).format('YYYY/MM/DD HH:mm');
   	                        else
   	                            return '';
   	                    }
	                }
	               ],
	            "drawCallback": function (oSettings) {
	                var k = 0;
	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	                }
	                   hqTaskTable.off('click', 'tr');
	                   hqTaskTable.on('click', 'tr', function (event) {
	                    event = event || window.event;
	                    var target = event.target || event.scrElement;
	                    var data = hqTaskTable.row(this).data();
	                    if (target.className.indexOf(hqMyBtn) != -1) {
	                    		hqMyTask(data);
	                    }
	                    return;
	                });
	             
	            }
	        });
	    });
  	 var hqMyTask=function(data){
  		var hqurl = window.rootPath + "/ui/beforecontract/edit/"
  		window.location=hqurl; 
  	 }
    	// 获取显示待处理信息分页
   	 var serverChanceData = {};
   	 var serverChanceTable;
   	    jQuery(document).ready(function () {
   	        serverChanceTable = jQuery('#chanceDealTable').DataTable({
   	            "serverSide": true,
   	            "ordering": false,
   	            "ajax": {
   	                "dataType": "json",
   	                "type": "get",
   	                "cache": false,
   	                "url":window.rootPath+ '/ui/index/getChanceDealPage',
   	                "data": function (d) {
   	                    return $.extend({}, d, serverChanceData);
   	                }
   	            },
   	           "iDisplayLength": 5,
   	            columns: [
   	                {
   	                    data: null,
   	                    defaultContent: ""
   	                },
   	                {
   	                    data: null,
   	                    defaultContent: "",
   	                    render: function (data, type, full, meta) {
   	                        var operate ='<button type="button" class="btn btn-sm btn-info dealBtn">处理</button>';
   	                        return operate;
   	                    }
   	                },
   	                {
   		                data: null,
   		                defaultContent: "",
	                    render: function (data, type, full, meta) {
	                    	var chanceType= data.chanceType;
	                    	var operate;
	                    	if(chanceType == 1){
	                    		operate ='<p>'+"信息跟踪"+'</p>';
	                    	}
	                    	else if(chanceType == 2){
	                    		operate ='<p>'+"信息分配员工"+'</p>';
	                    	}
	                    	else{
	                    		operate ='<p>'+"信息分配片区"+'</p>';
	                    	}
	                        return operate;
	                    }
   		                  
   	                },
   	                {
   		                data: 'chanceName'
   		                  
   	                },
   	                {
   	                    data: 'taskDate',
   	                    render:function (data, type, full, meta) {
   	                        if (data)
   	                            return moment(data).format('YYYY/MM/DD HH:mm');
   	                        else
   	                            return '';
   	                        }
   	                }
   	                ],
   	            "drawCallback": function (oSettings) {
   	                var k = 0;
   	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

   	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
   	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
   	                    k++;
   	                }
   	                   serverChanceTable.off('click', 'tr');
   	                   serverChanceTable.on('click', 'tr', function (event) {
	                    event = event || window.event;
	                    var target = event.target || event.scrElement;
	                    var data = serverChanceTable.row(this).data();

	                    if (target.className.indexOf('dealBtn') != -1) {
	                    	dealChance(data);
	                    }
	                    
	                 
	                    return;
	                });
   	             
   	            }
   	        });
   	    });
   	    //处理信息按钮
        var dealChance=function(data){
            var chanceType= data.chanceType;
            var url;
            if(chanceType == 1){
            	url=window.rootPath+'/ui/chanceReceive';
            }
            else if(chanceType == 2){
            	url=window.rootPath+'/ui/chanceDelivery/managerDelivery';
            }
            else{
            	url=window.rootPath+'/ui/chanceDelivery';
            }
            window.location = url;
        };
    // 获取显示公司发布信息数据 服务器分页
    var serverData = {};
    serverData.key='';
    var serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "iDisplayLength":4,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/index/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                    data: null,
                    defaultContent: ""// 排序
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {

                        var id=data.id
                        var operate ='<input type="checkbox" class="checkchild">';
                        return operate;

                    }
                },
                {
                    data: 'title'
                },
                {
                    data: 'type'
                },
                {
                    data: 'content'
                },
                {
                    data: 'attachNumber',
                    render: function (data, type, full, meta) {
                    	var dOut;
                    	if(data==null){
                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+0+'</span></td>'
                    	}else{
                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+data+'</span></td>'
                    	}
                    	return dOut;
                    }
                    
                },{
                    data: 'nWatchNumber',
                    render: function (data, type, full, meta) {
                    	var dOut;
                    	if(data==null){
                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+0+'</span></td>'
                    	}else{
                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+data+'</span></td>'
                    	}
                    	return dOut;
                    }
                    
                },
                {
                    data: 'user.saleArea.superior'
                },
                
                {
                    data: 'updateTime',
                    render : function(data,
                                      type, full,
                                      meta) {
                        if (data) {
                            return moment(data).format("YYYY-MM-DD");
                        }
                    }
                }
                ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable.row(this).data();

                    if (target.className.indexOf('fa-eye') != -1 || target.className.indexOf('cheBtn') != -1) {

                        cheModule(data);
                    }

                    return;
                });
            }
        });



        // 任务查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#keyword').val();
            serverTable.draw();
        })
        // 周报撤销查询
        jQuery('.resetBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });
        ///查看公司信息
        jQuery('.seeBtn').on('click', function () {
            var selects=getSelect("#dataTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能查看一条记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0){
                $.springLemonDialog({
                    content: '请选择要查看的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
            	var id = selects[0].id;
            	var url = window.rootPath + "/ui/news/witch?id="+id;
            	jQuery.ajax({
                    type: 'get',
                    url:  encodeURI(url),
                    data: JSON.stringify(),
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    success: function (data) {
                   }     
                });
                jQuery('#id').val(selects[0].id);
                jQuery('#title').val(selects[0].title);
                jQuery('#type').val(selects[0].type);
                jQuery('#content').val(selects[0].content);
                serverTable.draw();
                jQuery('#newsModal').modal('show');
            };
        });

// 下载附件
        jQuery(".download").on('click', function() {
            var selects=getSelect("#dataTable",1);
            var newsId=selects[0].id;
            filedown(newsId, '公司信息附件',1);
        });
    });
})();
